<template>
  <view class="container">
    <view class="name">{{app.app_name}}</view>
    <view class="desc">
      <view class="logo image">
        <image class="img" :src="baseUrl + app.logo.path" v-if="app.logo" />
      </view>
      <view class="title">欢迎使用{{app.app_name}}小程序</view>
      <view class="text">
        请确保你已满18岁并认真阅读 <text @click="onAgreement">用户协议</text>,
        我们将严格按照经您同意的各项条款收集、
        使用您的个人信息, 以便保障您的帐号安全并提供更好的服务.
      </view>
      <view class="go" @click="onLogin">同意并继续</view>
      <view class="deny" @click="onDeny">不同意</view>
    </view>
  </view>
</template>

<script>
import config from '../../config/config'
import User from '../../api/user'
import base64 from '../../util/base64'

const userModel = new User()

export default {
  data() {
    return {
      app: {},
      baseUrl: config.baseUrl
    }
  },
  onLoad() {
    this.getSet()
  },
  methods: {
    // 设置信息
    getSet() {
      userModel.getSet().then(response => {
        let { data } = response
        data = base64.parse(data)
        const { app } = data
        this.app = app
      })
    },
    // 协议
    onAgreement() {
      uni.navigateTo({
        url: '/sub_common/user/agreement'
      })
    },
    onLogin() {
      uni.login({
        provider: 'weixin',
        success: res => {
          // 获取code
          // console.log(res)
          if (res.code && res.code.length > 1) {
            userModel.loginMini({ code: res.code, referrer: userModel.getReferrer() }).then(response => {
              let { data } = response
              data = base64.parse(data)
              console.log(data)
              userModel.setSessionKey(data.session_key)
              // 用是vuex处理登录
              this.$store.dispatch('User/login', data.token).then(() => {
                if (!data.mobile || data.mobile.length < 1) {
                  uni.navigateTo({
                    url: '/sub_common/user/mobile'
                  })
                } else {
                  // uni.switchTab({
                  //   url: '/pages/center/index'
                  // })
                  // uni.navigateBack()

                  const serial = uni.getStorageSync('o')
                  if (serial) {
                    uni.navigateTo({
                      url: '/sub_main/pay/mini?serial=' + serial
                    })
                  } else {
                    uni.switchTab({
                      url: '/pages/center/index'
                    })
                  }

                }
              })


            })
          }
        }
      })
    },

    // 拒绝
    onDeny() {
      uni.switchTab({
        url: '/pages/index/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  background: #f3f5f7;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-bottom: 10rpx;
  align-items: center;
  min-height: 1000px;
}
.name {
  font-size: 60rpx;
  padding: 40px;
  font-weight: 700;
  letter-spacing: 5rpx;
  color: #333;
  opacity: 0.75;
}
.desc {
  height:400px;
  //width: 720rpx;
  border-radius: 20rpx;
  background: white;
  //padding: 100rpx 0 50rpx 0;
  position: fixed;
  left: 10px;
  right: 10px;
  bottom: 0;
  padding-top: 30px;
}

.go {
  background: $main_color;
  border-radius: 10rpx;
  height: 70rpx;
  line-height: 70rpx;
  width: 320rpx;
  margin: 30rpx auto;
  color: white;
  text-align: center;
}

.deny {
  /*background: red;*/
  text-align: center;
}

.title {
  font-size: 32rpx;
  font-weight: 700;
  margin: 40rpx 0;
  letter-spacing: 5rpx;
  text-align: center;
}

.text {
  font-size: 28rpx;
  color: #666;
  letter-spacing: 5rpx;
  line-height: 1.5em;
  padding: 0 50rpx;
  text-align: left;
}

.text text {
  color: #000;
  font-weight: 700;
}

.logo {
  width: 200rpx;
  height: 200rpx;
  /*background: red;*/
  margin: auto;
}

.logo image {
  width: 200rpx;
  height: 200rpx;
}

</style>
